#include <iostream>
#include "WMIdentify.hpp"
#include "globalParamInit.hpp"
#include "camera.hpp"
// #define DEBUG_ALL
// #define SHOW_TIME
// #define SHOW_WINDOWS
int main(int argc, char **argv)
{
        GlobalParam gp;
        address add;
        int color = BLUE;
        // 判断当前按键
        int key;
        // 初始化Glog并设置部分标志位
        google::InitGoogleLogging(argv[0]);
        // 设置Glog输出的log文件写在address中log_address对应的地址下
        FLAGS_log_dir = add.log_address;
        // 初始化gp
        initGlobalParam(gp, add, color);
        Camera cam(gp);
        cam.set_param_once(gp);
        cam.start_camera(gp);
        // 起始时间
        const int t_start = std::chrono::system_clock::now().time_since_epoch().count() / 1e9;
        WMIdentify WMI(gp);
        WMI.clear();
        int cnt = 0;
        int page = 0;
        int row = 0;
        int tag = 0; // 0为不改变，1为增加1，2为增加10，3为减少1，4为减少10
        double t_t = std::chrono::system_clock::now().time_since_epoch().count() / 1e9;
        while (1)
        {
                cv::Mat pic; // = cv::imread("/home/axi/图片/2.png");
#ifdef SHOW_TIME
                double dtime = std::chrono::system_clock::now().time_since_epoch().count() / 1e9;
#endif          // SHOW_TIME
                // 设置相机配置
                cam.set_param_mult(gp);
                // 取流获取图片，存入pic
                cam.get_pic(&pic, gp);
                //如果取流失败终止此次循环
                if (pic.empty() == 1)
                {
                        continue;
                }
                WMI.receive_pic(pic);
#ifdef SHOW_TIME
                std::cout << "receive_pic:" << std::chrono::system_clock::now().time_since_epoch().count() / 1e9 - dtime << std::endl;
#endif // SHOW_TIME
                double time = std::chrono::system_clock::now().time_since_epoch().count() / 1e9 - t_start;
#ifdef SHOW_TIME
                dtime = std::chrono::system_clock::now().time_since_epoch().count() / 1e9;
#endif // SHOW_TIME
                WMI.preprocess(gp);
#ifdef SHOW_TIME
                std::cout << "preprocess:" << std::chrono::system_clock::now().time_since_epoch().count() / 1e9 - dtime << std::endl;
                dtime = std::chrono::system_clock::now().time_since_epoch().count() / 1e9;
#endif // SHOW_TIME
                WMI.getContours(gp);
#ifdef SHOW_TIME
                std::cout << "getContours:" << std::chrono::system_clock::now().time_since_epoch().count() / 1e9 - dtime << std::endl;
                dtime = std::chrono::system_clock::now().time_since_epoch().count() / 1e9;
#endif // SHOW_TIME
                if (gp.get_armor_mode == HIERARCHY)
                {
                        WMI.getValidWingHalfHierarchy(gp);
                        WMI.selectWing(gp);
                        WMI.getValidWingHatHierarchy(gp);
                        WMI.selectWingHat(gp);
                }
                else if (gp.get_armor_mode == FLOODFILL)
                {
                        WMI.getArmorCenterFloodfill(gp);
                }
#ifdef SHOW_TIME
                std::cout << "getArmorCenter:" << std::chrono::system_clock::now().time_since_epoch().count() / 1e9 - dtime << std::endl;
                dtime = std::chrono::system_clock::now().time_since_epoch().count() / 1e9;
#endif // SHOW_TIME
                WMI.getVaildR(gp);
#ifdef SHOW_TIME
                std::cout << "getVaildR:" << std::chrono::system_clock::now().time_since_epoch().count() / 1e9 - dtime << std::endl;
                dtime = std::chrono::system_clock::now().time_since_epoch().count() / 1e9;
#endif // SHOW_TIME
                WMI.selectR(gp);
                WMI.windowsManager(gp, page, row, tag);
                tag = 0;
#ifdef SHOW_TIME
                std::cout << "selectR:" << std::chrono::system_clock::now().time_since_epoch().count() / 1e9 - dtime << std::endl;
                dtime = std::chrono::system_clock::now().time_since_epoch().count() / 1e9;
#endif // SHOW_TIME
                WMI.updateList(gp, time);
#ifdef SHOW_TIME
                std::cout << "updateList:" << std::chrono::system_clock::now().time_since_epoch().count() / 1e9 - dtime << std::endl;
                dtime = std::chrono::system_clock::now().time_since_epoch().count() / 1e9;
#endif // SHOW_TIME
                key = cv::waitKey(1);
                cnt++;
#ifdef SHOW_TIME
                std::cout << "单次循环耗时： " << std::chrono::system_clock::now().time_since_epoch().count() / 1e9 - time - t_start << std::endl;
                std::cout << "平均循环耗时： " << (std::chrono::system_clock::now().time_since_epoch().count() / 1e9 - t_t) / cnt << std::endl;
                std::cout << "平均帧率： " << (double)cnt / (std::chrono::system_clock::now().time_since_epoch().count() / 1e9 - t_t) << std::endl;
#endif // SHOW_TIME
                if (key == 27)
                {
                        LOG_IF(INFO, gp.switch_INFO) << "平均循环耗时：" << (std::chrono::system_clock::now().time_since_epoch().count() / 1e9 - t_t) / cnt;
                        LOG_IF(INFO, gp.switch_INFO) << "平均帧率： " << (double)cnt / (std::chrono::system_clock::now().time_since_epoch().count() / 1e9 - t_t);
                        return 0;
                }
                if (key == 's' || key == 'S')
                {
                        initGlobalParam(gp, add, color);
                }
                if (key == 'r' || key == 'R')
                {
                        if (row > 0)
                                row--;
                }
                if (key == 'f' || key == 'F')
                {
                        if (row < 5)
                                row++;
                }
                if (key == 'z' || key == 'Z')
                {
                        if (page > 0)
                                page--;
                }
                if (key == 'c' || key == 'C')
                {
                        if (page < 4)
                                page++;
                }
                if (key == 'u' || key == 'U')
                {
                        tag = 1;
                }
                if (key == 'j' || key == 'J')
                {
                        tag = 3;
                }
                if (key == 'i' || key == 'I')
                {
                        tag = 2;
                }
                if (key == 'k' || key == 'K')
                {
                        tag = 4;
                }
                if (key == 'q' || key == 'Q')
                {
                        saveGlobalParam(gp, add, color);
                }
        }
        cv::waitKey();
        return 0;
}